package edu.ustb.mis.dm.simpleTest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import edu.ustb.mis.dm.util.Utils;

public class MushroomChange {
	public static Integer NUM = 1;

	public static void main(String[] args) throws Exception {
		MushroomChange mrc = new MushroomChange();
		List<String[]> list = mrc.read();
//		mrc.stand1(list);
//		mrc.stand2(list);
//		mrc.stand3(list);
//		mrc.stand4(list);
		list = mrc.buildMap(list);
		//list = mrc.fill(list);
		mrc.writefile(list);
	}

	public List<String[]> fill(List<String[]> list) {
		Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
		Map<String, String> map_ = new HashMap<String, String>();
		Map<String, Integer> subMap;
		for (String[] sa : list) {
			if (!map.containsKey(sa[0])) {
				if (!sa[11].equals("?")) {
					subMap = new HashMap<String, Integer>();
					subMap.put(sa[11], 1);
					map.put(sa[0], subMap);
				}
			} else {
				if (!sa[11].equals("?")) {
					subMap = map.get(sa[0]);
					if (subMap.containsKey(sa[11])) {
						subMap.put(sa[11], subMap.get(sa[11]) + 1);
					} else {
						subMap.put(sa[11], 1);
					}
				}
			}
		}
		for (Entry<String, Map<String, Integer>> entry : map.entrySet()) {
			for (Entry<String, Integer> entry_ : entry.getValue().entrySet()) {
				if (!map_.containsKey(entry.getKey())) {
					map_.put(entry.getKey(), entry_.getKey());
				} else {
					if (entry_.getValue() > entry.getValue().get(map_.get(entry.getKey()))) {
						map_.put(entry.getKey(), entry_.getKey());
					}
				}
			}
		}
		for (String[] sa : list) {
			if (sa[11].equals("?")) {
				sa[11] = map_.get(sa[0]);
			}
		}
		return list;
	}

	public List<String[]> read() throws IOException {
		List<String[]> list = new ArrayList<String[]>();
		final Reader reader = new BufferedReader(new FileReader("input/zoo-old.txt"));
		String instanceString = Utils.readLine(reader);
		while (instanceString != null) {
			list.add(instanceString.split(","));
			instanceString = Utils.readLine(reader);
		}
		reader.close();
		return list;
	}
	public void stand4(List<String[]> list){
		for (String[] sa : list) {
			double s = Double.valueOf(sa[3]);
			if(s<=0.5){
				sa[3]="1";
			}else if(s>0.5&&s<=1.0){
				sa[3]="2";
			}else if(s>1.0&&s<=1.5){
				sa[3]="3";
			}else if(s>1.5&&s<=2.0){
				sa[3]="4";
			}else{
				sa[3]="5";
			}
		}
	}
	
	public void stand3(List<String[]> list){
		for (String[] sa : list) {
			double s = Double.valueOf(sa[2]);
			if(s<=1.4){
				sa[2]="1";
			}else if(s>1.4&&s<=2.0){
				sa[2]="2";
			}else if(s>=3.0&&s<=3.5){
				sa[2]="3";
			}else if(s>3.5&&s<=4.0){
				sa[2]="4";
			}else if(s>4.0&&s<=4.5){
				sa[2]="5";
			}else if(s>4.5&&s<=5.0){
				sa[2]="6";
			}else if(s>5.0&&s<=5.5){
				sa[2]="7";
			}else if(s>5.5&&s<=6.0){
				sa[2]="8";
			}else if(s>6.0&&s<=6.5){
				sa[2]="9";
			}else{
				sa[2]="10";
			}
		}
	}
	public void stand2(List<String[]> list){
		for (String[] sa : list) {
			double s = Double.valueOf(sa[1]);
			if(s>1.7&&s<=2.2){
				sa[1]="1";
			}else if(s>2.2&&s<=2.7){
				sa[1]="2";
			}else if(s>2.7&&s<=3.2){
				sa[1]="3";
			}else if(s>3.2&&s<=3.7){
				sa[1]="4";
			}else if(s>3.7&&s<=4.2){
				sa[1]="5";
			}else{
				sa[1]="6";
			}
		}
	}
	
	public void stand1(List<String[]> list){
		for (String[] sa : list) {
			double s = Double.valueOf(sa[0]);
			if(s>4&&s<=4.7){
				sa[0]="1";
			}else if(s>4.7&&s<=5.2){
				sa[0]="2";
			}else if(s>5.2&&s<=5.7){
				sa[0]="3";
			}else if(s>5.7&&s<=6.2){
				sa[0]="4";
			}else if(s>6.2&&s<=6.7){
				sa[0]="5";
			}else if(s>6.7&&s<=7.2){
				sa[0]="6";
			}else{
				sa[0]="7";
			}
		}
	}

	public List<String[]> buildMap(List<String[]> list) {
		int length = list.get(0).length-2;
		for (int i = 0; i < length; i++) {
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (String[] l : list) {
				if (!map.containsKey(l[i]) && !l[i].equals("?")) {
					map.put(l[i], NUM);
					NUM++;
				}
			}
			for (String[] l : list) {
				if (!l[i].equals("?")) {
					l[i] = String.valueOf(map.get(l[i]));
				}
			}
		}
		return list;
	}

	private void appendOutPutString(final StringBuffer sbf, final int i, final String[] sa) {
		sbf.append(sa[sa.length-1]).append(sa[sa.length-2]).append(",");
		for (int j = 0; j < sa.length-2; j++) {
			//if (!sa[j].equals("?")) 
			sbf.append(sa[j]).append(",");
		}
		sbf.append("\r\n");
	}

	public void writefile(final List<String[]> list) {
		try {
			final BufferedWriter bw = new BufferedWriter(new FileWriter("output/zoo-old.txt"));
			final StringBuffer sbf = new StringBuffer();
			final int size = list.size();
			String[] sa;
			for (int i = 0; i < size; i++) {
				sa = list.get(i);
				appendOutPutString(sbf, i, sa);
			}
			bw.write(sbf.toString());
			bw.flush();
			bw.close();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}
}
